<?php
/**
 * This class implements a connection to del.icio.us
 *
 * @author Thomas Kappel
 * @copyright Copyright (c) 2008
 * @version $Id: Abstract.php 1877 2008-02-19 18:09:30Z tkappel $
 */

function aboutDelicious() {
  return array(
    "name"    => "Del.icio.us"
  );
}

require ($this->_getPluginRootDir()."/lib/Networks/Delicious/DeliciousClient.php");

class Delicious extends Network_Abstract implements findContactsOfContacts {
	public $_Name            = "Delicious";   // string:          name of the network
	public $user_id          = 0;       // string:          user id within the network
  public $me               = false;   // array:           all information about the user(account) himself 
  public $contacts         = false;   // array of arrays: data of contacts 
  public $delicious_client = false;
  public $username         = false;

	public $activation_link  = false;   // string: url of the login page (optional)
	
	protected static $_instance  = null;
  
	public $errors = array(
		1=>'You must enter credentials',
		2=>'You have been throttled. Please stop attempting to connect to del.icio.us',
		3=>'The del.icio.us service did not return a proper HTTP response',
		4=>'Unable to connect to del.icio.us',
		5=>'Unable to send data to del.icio.us',
		6=>'Unable to parse the XML returned from del.icio.us' );

  public function supports($method) {
    switch($method) {
      case "find_friends_of_friends"         : return true;
      case "multiple_accounts"               : return false; // one person should have only one account 
      case "require_requests_for_friendship" : return false; // no need to send these messages
      case "save_friends_to_network"         : return true;
      case "search_members"                  : return false; // find account of a person you already know from other networks
      case "disconnect"                      : return false;
      default                                : return false;
    }
  }
  
  public function setAsFriend($accountName, $messageToUser = "") {
    if(!is_string($accountName) || !strlen($accountName)) return false;
    // delicious does not support sending messages to users
    
    //first get key from the users page
    $result = $this->delicious_client->post_request($accountName);
    $start_of_link = "p_frischmuth?networkadd=".$accountName.";key=";
    $start = strpos($result, $start_of_link) + $start_of_link;
    $key = substr($result, $start,32);
    
    //initiate contact
    $this->delicious_client->post_request($start_of_link . $key);
  }
		
  public function __construct($account = false) {
  	$this->NetworkName="Delicious";
  	$this->account = $account ? $account : false;
  	self::$_instance = $this;
  }

	/*
	 * @returns URL of the Social Network Service
	 */  
  public function getServiceHomepage() {
    return "http://delicious.com";
  }

	/*
	 * @returns associative array of arrays:
	 *  "mandatory" => required fields for logging in (for example: "uri", "user", "pass")
	 *  "optional"  => some more allowed fields
	 */  
  public function getAccountingRequirements() {
    return array( // password still not needed
    	"mandatory" => array("user"),
    	"optional"  => array()
    );
  }

  public function require_connect_button() {
    return false;
  }

	/*
	 * establishes connection to the network
	 * 
	 * @param: URI, username, password (all optional, depending on the network)
	 * @returns: information about the user or false on error
	 */
	public function connect($URI="", $user="", $pass="") {
//    print "###" . strlen($user);
		if(!strlen($user)) return false;
//    print "###";
	  $del = new DeliciousClient($user,$pass);
//    print "###";
	  if(!$del) return false;
//    print "###";

	  $this->username = $user;
	  $this->delicious_client = $del;
    $this->accountName = $user;
//    print "OK.";

//    $session = Zend_Registry::get('session');
//    $session->delicious->user_id = $this->username;
//    $session->delicious->client = $this->delicious_client;
	  return true;
	}

	/*
	 * @returns true if connection is already established
	 */
	public function connected() {
    return true;
	}

	/*
	 * close connection
	 * 
	 * @returns: boolean (true on success)
	 */
	public function disconnect() {}
	 
  public function fetchContacts() {
    if(!$this->delicious_client) $this->delicious_client = new DeliciousClient("unknown","unknown");
    $contacts = $this->delicious_client->call_method("fetchcontacts",$this->account["accountName"]);
    return $contacts;
    /*
    $max_contacts = 100;
    $counter = 1;
    foreach($contacts as $uid) {
      $this->fetchContact($uid);
      $counter++;
      if($counter>$max_contacts) break;
    }
    return $this->contacts;
    */
  }
	
	/**
	 * @param: id of a contact
	 * @return: standardised associative array of information about the user
	 *           - foaf attributes
	 *           - one element with the key "full_data" contains all fetchable information
	 * @return associative array or false on error
	 */
	function fetchContact($uid) {
	  if(!$this->delicious_client) $this->delicious_client = new DeliciousClient("unknown","unknown");
    $contact = $this->delicious_client->call_method("fetchcontact",$uid);
    $this->contacts[$uid]=array("accountName" => $uid);
    if($contact!==false) {
      if(isset($contact["homepage"]) && strlen($contact["homepage"])) $this->contacts[$uid]["homepage"] = $contact["homepage"];
      if(isset($contact["name"])     && strlen($contact["name"]))     $this->contacts[$uid]["name"]     = $contact["name"];
    }
    return $this->contacts[$uid];
	}

	/*
	 * @param: id of a contact
	 * @returns: array of user ids of contacts of the user
	 * @returns false on error 
	 */	
	public function fetchContactsOfContacts($uid) {
    if(!$this->delicious_client)  $this->delicious_client = new DeliciousClient("unknown","unknown");
    $contacts = $this->delicious_client->call_method("fetchcontacts",$uid);
    $max_contacts = 100;
    $counter = 1;
    foreach($contacts as $uid) {
      $this->fetchContact($uid);
      $counter++;
      if($counter>$max_contacts) break;
    }
    return $this->contacts;
	}

	/*
	 * @param: contact information (as returned by fetchContact())
	 * @returns: unique identifier of this account within the network
	 */
	public function getAccountIdentifier($contact) {
	  return $contact["accountName"];
	}

	/*
	 * @returns object: instance of this class
	 */
  public static function getInstance() {
    if (null === self::$_instance) return false;
    return self::$_instance;
  }

	/*
	 * @param   string: account name 
	 * @param   string: another account name 
	 * @returns boolean: true, if the contacts are contacts of each other
	 */
	public function areContacts($contact1, $contact2) {
	  return false;
	}
}
?>
